package com.xw.userService.web;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import com.xw.userService.dto.ResultDto;
import com.xw.userService.dto.request.UserInfoRequestDTO;
import com.xw.userService.dto.response.UserInfoResponseDTO;
import com.xw.userService.enums.ResultCodeEnum;
import com.xw.userService.model.UserInfo;
import com.xw.userService.service.UserInfoService;

@RestController
@RequestMapping("user")
public class TestController {
	private static final Logger LOG = LoggerFactory.getLogger(TestController.class);
	@Autowired
	private UserInfoService userInfoService;

	@RequestMapping(value = "/loadById", method = RequestMethod.POST)
	@HystrixCommand(fallbackMethod = "loadByIdFB")
	public ResultDto<UserInfoResponseDTO> loadById(@RequestBody UserInfoRequestDTO userInfoRequestDTO) {
		ResultDto<UserInfoResponseDTO> resultDto = new ResultDto<>();
		try {
			LOG.info("查询用户 > > > 入参：{}", userInfoRequestDTO.toString());
			UserInfo userInfo = this.userInfoService.loadByUserId(userInfoRequestDTO.getUserId());
			UserInfoResponseDTO userInfoResponseDTO = new UserInfoResponseDTO();
			if (userInfo!=null) {
			BeanUtils.copyProperties(userInfo, userInfoResponseDTO);
			}
			resultDto.setObject(userInfoResponseDTO);
			resultDto.setCode(ResultCodeEnum.System.SUCCESS.getCode());
			resultDto.setMessage(ResultCodeEnum.System.SUCCESS.getMsg());
			LOG.info("查询用户 > > > 结束：{}", userInfoResponseDTO.toString());
			return resultDto;
		} catch (Exception e) {
			LOG.info("查询用户 > > > 失败");
			resultDto.setCode(ResultCodeEnum.System.PARAM_ERROR.getCode());
			resultDto.setMessage(ResultCodeEnum.System.PARAM_ERROR.getMsg());
			return resultDto;
		}

	}

	public ResultDto<UserInfoResponseDTO> loadByIdFB(@RequestBody UserInfoRequestDTO userInfoRequestDTO) {
		LOG.info("查询用户 > > > 入参：{}", userInfoRequestDTO.toString());
		ResultDto<UserInfoResponseDTO> resultDto = new ResultDto<>();
		resultDto.setCode(ResultCodeEnum.System.SYSTEM_ERROR.getCode());
		resultDto.setCode(ResultCodeEnum.System.SYSTEM_ERROR.getMsg());
		return resultDto;

	}
}
